const { list, find, deleteRecord } = require('./data/devices')
const Mock = require('mockjs')

const base = '/userserver'

module.exports = [{
  url: `${base}/devices/total`,
  type: 'get',
  response: config => {
    return { success: true, data: Mock.Random.integer(800, 1000) }
  }
}, {
  url: `${base}/devices`,
  type: 'get',
  response: config => {
    const { page, limit, name, orgUnitId, roomId } = config.query
    return list(page, limit, name, orgUnitId, roomId)
  }
}, {
  url: `${base}/devices/[1-9][0-9]*`,
  type: 'put',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    const r = find(id)
    if (r) {
      const { name } = config.body
      r.name = name
      return { success: true }
    } else {
      return { success: false, message: '记录不存在' }
    }
  }
}, {
  url: `${base}/devices/[1-9][0-9]*`,
  type: 'delete',
  response: config => {
    const id = config.url.match(/[1-9][0-9]*/)
    deleteRecord(id)
    return { success: true }
  }
}]
